package Core
{
	internal class AllVars implements ISerializable
	{
	private var _varObj:Object;
	
	public function AllVars( initObj:Object=null ) {
		if(initObj != null) {
// XXX???		if( initObj.hasOwnVariable("objectID")) { // objectID
			// Read in the Vars from storage
			// Vars can give database access params
		} else { // Create a new one
			_varObj = new Object();
			_varObj.varnumberObj = new Object();
			_varObj.varstringObj = new Object();
			// XXX Booleans should be only strings that represent TRUE bools. TRUE test tests only for existence. When varbool is set to FALSE it is erased.
			_varObj.varbooleanObj = new Object();
		}
	}
	
	// Add new variable type - ie
	public function addVarType(varType:String):void {
		if( ! _varObj.hasOwnProperty(varType+"Obj") ) {
			_varObj[varType+"Obj"] = new Object();
		}
	}
	public function destroyVarType(varType:String):void {
		_varObj[varType+"Obj"] = new Array();
	}
	// Destroys a var if it exists.
	public function destroyVar(varType:String,varName:String):void {
		delete _varObj[varType+"Obj"][varName];
	}

	// Stick a value paramArr[1] in a named var paramArr[0] 
	// of type varType
	// Creates or overwrites
	public function setVar(varType:String ,paramArr:Array ):void {
	// associative array of associative arrays
		_varObj[varType+"Obj"][paramArr[0]] = paramArr[1];
	
	}
	
	public function getVar(varType:String,varName:String) :Object{
		if(_varObj[varType+"Obj"].hasOwnProperty(varName)) {
			return _varObj[varType+"Obj"][varName];
		}
		return null;
	}
	
	public function getVartype(varType:String):Array {
		var resultArr:Array = new Array();
		
		for( var varI:String in _varObj[varType+"Obj"]) {
			resultArr.push({varname:String(varI),value:_varObj[varType+"Obj"][varI]});
		}
		return resultArr;
	}
	
		
	// Increment an existing varnumber
	// if it doesn't exist fail silently
	public function incrementVarNumber(varName:String, inc:Number):void {
		if(_varObj["varnumberObj"].hasOwnProperty(varName)) {
			_varObj["varnumberObj"][varName]+= inc;
		}
	}
	
	public function evaluate(command:String, paramArr:Array):Number {
		var branch:Number = 0; // Default to branch 0
		switch(command) {
			case "ifvarnumberless":
				// Does the property exist?
				if(_varObj["varnumberObj"].hasOwnProperty(paramArr[0])) {
					if(	_varObj["varnumberObj"][paramArr[0]] >= paramArr[1]) {
						// If it's not less take "NOT" branch
						branch = 1;
					}
				}
			break;
			case "ifvarnumbermore":
				// Does the property exist?
				if(_varObj["varnumberObj"].hasOwnProperty(paramArr[0])) {
					if(	_varObj["varnumberObj"][paramArr[0]] <= paramArr[1]) {
						// If it's not less take "NOT" branch
						branch = 1;
					}
				}
			break;
			case "ifvartypeequal":
				// Does the property exist?
				if(_varObj[paramArr[2]+"Obj"].hasOwnProperty(paramArr[0])) {
					if(	_varObj[paramArr[2]+"Obj"][paramArr[0]] != paramArr[1]) {
						// If it's not less take "NOT" branch
						branch = 1;
					} else {
// trace("DramVar.evaluate: TRUE: "+paramArr);
					}
				}
			break;	
			default:
trace("DramVar.evaluate can't parse command: "+command);
			break;
		}
		return branch;
	}
	
	// Data base functions ------------------------------------------
	public function read(paramObj:Object):String {
		var result_st:String = "success";
trace("DramVars.read() not implemented.");		
		return result_st;
	}
	public function write(paramObj:Object):String {
		var result_st:String = "success";
trace("DramVars.write() not implemented.");		
		return result_st;
	}
	}
}